package com.magnificent.atlantis.client.data.configuration;

import com.magnificent.atlantis.client.data.settings.User;

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * User: Babanin
 * Date: 16.02.2009
 * Time: 16:26:20
 */
@XmlRootElement(name = "serverConfiguration")
public class ServerConfiguration extends AbstractConfiguration {

    private int port;
    private String host;
    private Date lastUpdate;

    private List<User> users;

    private Date lastUserUpdate;
    private Date lastMailUpdate;
    private Date lastTaskUpdate;

    private String lastLogin;

    public ServerConfiguration() {
        lastUpdate = new Date(0);
    }

    @XmlAttribute()
    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    @XmlAttribute()
    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    @XmlAttribute()
    public Date getLastUpdate() {
        return lastUpdate;
    }

    public void setLastUpdate(Date lastUpdate) {
        this.lastUpdate = lastUpdate;
    }

    @XmlElement()
    public Date getLastUserUpdate() {
        return lastUserUpdate;
    }

    public void setLastUserUpdate(Date lastUserUpdate) {
        this.lastUserUpdate = lastUserUpdate;
    }

    @XmlElement()
    public Date getLastMailUpdate() {
        return lastMailUpdate;
    }

    public void setLastMailUpdate(Date lastMailUpdate) {
        this.lastMailUpdate = lastMailUpdate;
    }

    @XmlElement()
    public Date getLastTaskUpdate() {
        return lastTaskUpdate;
    }

    public void setLastTaskUpdate(Date lastTaskUpdate) {
        this.lastTaskUpdate = lastTaskUpdate;
    }

    public List<User> getUsers() {
        if (users == null) {
            users = new ArrayList<User>();
        }

        return users;
    }

    public void setUsers(List<User> users) {
        this.users = users;
    }

    public User getUser(int id) {
        for (User user : users) {
            if (user.getId() == id) {
                return user;
            }
        }

        return null;
    }

    public List<User> getUsers(List<Integer> ids) {
        List<User> result = new ArrayList<User>();

        if (ids == null) {
            return result;
        }

        for (Integer id : ids) {
            User u = getUser(id);
            if (u != null) {
                result.add(u);
            }
        }

        return result;
    }
}
